package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.AccountMapper;
import com.example.demo.pojo.Account;
import com.example.demo.pojo.Menu;
import com.example.demo.service.AccountService;
import com.example.demo.service.MenuService;
import com.example.demo.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private MenuService menuService;


    @Override
    public Map<String, Object> login(Account account) {
        QueryWrapper<Account> qw=new QueryWrapper<>();
        if(account.getUsername()!=null){
            qw.eq("username",account.getUsername());//数据库查询账户
        }
        if(account.getPassword()!=null){
            qw.eq("password",account.getPassword());//数据库查询密码
        }


        Account account1=accountMapper.selectOne(qw);
        if(account1!=null){
            Map<String,Object>map=new HashMap<>();
            String token=jwtUtil.createToken(account1);//配合前端转为token
//            {"code":20000,"data":{"token":"admin-token"}}前端响应格式
            map.put("token",token);
            return map;
        }
        return null;
    }

    @Override
    public Map<String, Object> getUserInfo(String token) {
        Account account=jwtUtil.parseToken(token,Account.class);
        Map<String,Object>data=new HashMap<>();
        if(account!=null){
            data.put("role", new Array[0]);
            data.put("introduction",account.getRole());
            data.put("avatar","aaa");
            data.put("name",account.getUsername());

            List<Menu> menuListByUserId = menuService.getMenuListByUserId(account.getId());
            data.put("menuList",menuListByUserId);
            return data;
        }
        return null;
    }

//    @Override
//    public Map<String, Object> getUserInfo(String token) {
//        // 根据token获取用户信息，redis
//        /*Object obj = redisTemplate.opsForValue().get(token);*/
//        Account loginUser=null;
//        try {
//            loginUser = jwtUtil.parseToken(token, Account.class);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if(loginUser != null){
//            /*Account loginUser = JSON.parseObject(JSON.toJSONString(obj),Account.class);*/
//            Map<String, Object> data = new HashMap<>();
//            data.put("name",loginUser.getUsername());
//            data.put("avatar", loginUser.getAvatar());
//
//            // 角色
//            //List<String> roleList = this.baseMapper.getRoleNameByUserId(loginUser.getId());
//            data.put("roles",loginUser.getRole());
//
//            //权限列表
//            List<Menu> menuList = menuService.getMenuListByUserId(loginUser.getId());//这里的得到的就是当前用户所拥有的菜单列表
//            data.put("menuList",menuList);//存到map里返回给前端
//            return data;
//        }
//        return null;
//    }

    @Override
    public void logout(String token) {

    }
}
